package com.mht.cfa.server.contexts;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;


@EnableAutoConfiguration
@ImportResource("classpath:spring/applicationContext.xml")
public class SpringContext {

	private static ConfigurableApplicationContext context;

	public static final Map<String, Long> counter = new ConcurrentHashMap<>();


	public static void start(String[] args) {
		if (null == context) {
			if (args == null) {
				context = SpringApplication.run(SpringContext.class);
			} else {
				context = SpringApplication.run(SpringContext.class, args);
			}
		}
	}

	public static <T> T getBean(Class<T> targetClass) {
		if (null == context) {
			start(null);
		}
		return context.getBean(targetClass);
	}

	public static <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
		return context.getBeansOfType(type);
	}

}
